package com.nd.qa.shareplate.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import org.springframework.stereotype.Component;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;
@Component
public class UCTools {
    /**
     * 获取bearer_tokens授权信息
     * 
     * @param uri
     *            uid
     * @param password
     *            密码
     * @return 
     * {
           "access_token": "E379FA2E010AD662736CFCCC0C4E7D5F1E8439D04580F800A477F6D09FAED2216AEA2BF9579B5362",
           "expires_at": "2016-06-17T14:55:21.848+0800",
           "mac_algorithm": "hmac-sha-256",
           "mac_key": "wiEW8EwHpc",
           "refresh_token": "E379FA2E010AD66297364B51826883D60BF80E6B0AE4962DC96A49D91CADB298B893E0CA93810B11",
           "server_time": "2016-05-18T14:55:21.855+0800",
           "user_id": 2107168459
        }
     */
    public String get_bearer_token() {
        String result = "";
        String authorization = "";
        String _url = "https://ucbetapi.101.com/v0.93/bearer_tokens";
            try {
                //创建连接 
                URL url = new URL(_url); 
                
                HttpsURLConnection connection = (HttpsURLConnection) url 
                        .openConnection(); 
                connection.setDoOutput(true); 
                connection.setDoInput(true); 
                connection.setRequestMethod("POST"); 
                connection.setUseCaches(false); 
                connection.setInstanceFollowRedirects(true); 
                connection.setRequestProperty("Accept", "application/json"); 
                connection.setRequestProperty("Content-Type", "application/json"); 
                connection.connect(); 

                //POST请求 
                DataOutputStream out = new DataOutputStream( 
                        connection.getOutputStream()); 
                JSONObject obj = new JSONObject(); 
                obj.put("login_name", "waf_loginer"); 
                obj.put("password", "80fba977d063a6f7262a8a9c95f61140"); 
                out.writeBytes(obj.toString()); 
                out.flush(); 
                out.close(); 
                System.out.println(connection.getResponseMessage());
                //读取响应 
                BufferedReader reader = new BufferedReader(new InputStreamReader( 
                        connection.getInputStream())); 
                String lines; 
                //StringBuffer sb = new StringBuffer(""); 
                while ((lines = reader.readLine()) != null) { 
                    lines = new String(lines.getBytes(), "utf-8"); 
                    //result.append(lines); 
                    result += lines;
                } 
                System.out.println(result); 
                reader.close(); 
                // 断开连接 
                connection.disconnect(); 
                //拼装数据
                JSONObject resultJsonObject = JSONObject.fromObject(result);
                String access_token = resultJsonObject.getString("access_token");
                authorization = "Bearer \"" + access_token +"\"";
                
            } catch (MalformedURLException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (UnsupportedEncodingException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            return authorization;
    }
}
